﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogCat.Utility;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace LogCat
{
    public partial class Main : Form
    {
        private const string dbname = "logcat.db";

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            CreateDb();
            AutoSize();
        }

        private void openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            LogFilePath = openFileDialog.FileName;
            LoadData(LogFilePath);
            ShowData(ConfigUtility.config.sql_defult);
        }

        //数据显示到界面
        private bool ShowData(string sql) 
        {
            OleDbDataReader data;
            try
            {
                data = DBUtility.Select(sql);
            }
            catch 
            {
                return false;
            }

            DataTable dt = new DataTable();
            dt.Load(data);
            this.dataGridView1.DataSource = dt;

            return true;
        }

        private void Main_ClientSizeChanged(object sender, EventArgs e)
        {
            AutoSize();
        }

        //调整控件大小
        private void AutoSize() {
            tab.Width = this.Width - Box_main.Location.X - tab.Location.X - 30;
            tab.Height = this.Height - Box_main.Location.Y - tab.Location.Y - 80;
            dataGridView1.Width = tab.Width;
            dataGridView1.Height = tab.Height;
        }

        private void Text_Sql_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter){
                if (!ShowData(Text_Sql.Text)) {
                    Text_Sql.BackColor = Color.Red;
                }
            }
        }

        private void Text_Sql_TextChanged(object sender, EventArgs e)
        {
            Text_Sql.BackColor = Color.White;
        }


        //当前日志文件路径
        public static string LogFilePath = "";

        //创建数据库
        public static void CreateDb() {
            string dbpath = IOUtility.DataPath + dbname;
            //删除已经存在的access数据库
            if (!IOUtility.DeleteFile(dbpath))
            {
                DialogResult dr = MessageBox.Show("文件操作失败,无法启动应用！", "Close", MessageBoxButtons.OK);
                if (dr == DialogResult.OK)
                {
                    Application.Exit();
                }
            }
            //创建access数据库
            DBUtility.CreateDB(dbpath);
            //创建表
            Match mt = new Regex(ConfigUtility.config.log_rule).Match(ConfigUtility.config.log_raw);

            List<string> fields = new List<string>();
            for (int i = 1; i < mt.Groups.Count; i++)
            {
                fields.Add("field" + i);
            }
            DBUtility.CreateTable("log", fields.ToArray()); 
        }

        //加载数据
        public static void LoadData(string path){
            if (path.Equals(""))
            {
                return;
            }

            StringParseUtility.ParseFile(path, delegate(Match mt)
            {
                Dictionary<string, string> data = new Dictionary<string, string>();

                for (int i = 1; i < mt.Groups.Count; i++)
                {
                    data.Add("field" + i, mt.Groups[i].Value);
                }
                DBUtility.Insert("log", data);
            });
        }

        private void menu_open_Click(object sender, EventArgs e)
        {
            this.openFileDialog.ShowDialog();
        }

        private void menu_config_Click(object sender, EventArgs e)
        {
            new Config().Show();
        }

        private void menu_about_Click(object sender, EventArgs e)
        {

        }
    }
}
